import os
import time

import yaml

# from zwz.zwzApi.api.util import generateNonce, generateTimestamp, generateSignature
from zwz.tokit.api.util import signature


class ReadYaml(object):
    def __init__(self):
        self.signature = signature()
    def read_yml(self):
        # 获取配置文件路径
        curPath = os.path.abspath(os.path.dirname(__file__))
        yamlPath = os.path.abspath(os.path.dirname(curPath) + os.path.sep + "datas/api_config.yml")
        # 将signature、nonce、timestamp，转换成正常数据
        f = open(yamlPath, 'r', encoding='utf-8')

        content = f.read()
        contents = yaml.load(content, Loader=yaml.FullLoader)
        '''
        print("文件中所有内容：", contents)
        print("登录接口内容：", contents['login'])
        '''
        # print(contents)
        return contents

    def get_url(self, api_name):
        content = self.read_yml()
        new_url = content['host'] + content[api_name]['url']
        # print("请求地址： ", new_url)
        return new_url

    def get_method(self, api_name):
        content = self.read_yml()
        # print("请求方式： ", content[api_name]['method'])
        return content[api_name]['method']

    def get_data(self, api_name):
        content = self.read_yml()
        # print("请求数据： ", content[api_name]['data'])
        return content[api_name]['data']

    def get_headers(self, api_name):
        from zwz.zwzApi.action.get_token import Get_token
        # 获取yaml文件内容
        content = self.read_yml()
        # content[api_name]['headers']['nonce'] = str(generateNonce())
        # content[api_name]['headers']['timestamp'] = str(generateTimestamp())
        met = content[api_name]['method']
        url = content[api_name]['url']
        url = url.split('?')[0]
        # print(str(met).upper())
        # print(url)
        content[api_name]['headers']['signature'] = str(
            self.signature.generateSignature(str(met).upper(), url))
        if 'token' in content[api_name]['headers']:
            content[api_name]['headers']['token'] = Get_token().getToken()
            # content[api_name]['headers']['token'] = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJlbnZpcm9ubWVudCI6InByb2QiLCJ0b2tpdElkIjoxMTczNSwiaXNzIjoiVE9LSVQiLCJ1c2VyTmFtZSI6IlRPS0lUXzExNzM1IiwidXVpZCI6IjhhNTU4Nzg0LWIzMmUtNDgzZC1hMzFiLWM3MWE4MDczYTQ4YSJ9.DXXOk99L3WH2rIzKc5uJLudfAWjibosOeE6yxkDamJs'
        return content[api_name]['headers']

    # 获取响应结果的预期结果
    def get_expected(self, api_name):
        content = self.read_yml()
        expected = content[api_name]['expected']
        # print(expected, type(expected), expected["success"], expected["decimal"])
        return expected

    def get_time(self):
        # 以毫秒为单位的时间戳
        get_now_milli_time = int(time.time() * 1000)
        print(get_now_milli_time)


if __name__ == "__main__":
    # read_yml()
    ry = ReadYaml()
    # ry.is_login("login_prod")
    print(ry.get_headers('login'))
